﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Telewave.SoftwareRegister;

namespace OrderSystem
{
    public partial class FrmRegister : Form
    {
        private bool isClosed;

        public bool IsClosed
        {
            get { return isClosed; }
            set { isClosed = value; }
        }

        public FrmRegister()
        {
            InitializeComponent();
            this.Load += FrmRegister_Load;
        }

        private void FrmRegister_Load(object sender, EventArgs e)
        {
            try
            {
                this.TopMost = true;
                this.txtMercId.Text = SystemInfo.GetCpuId();
                this.txtMercId.Enabled = true;
                if (!SystemInfo.CheckContainsMac(this.txtMercId.Text))
                {
                    lbResult.Text = "您尚未注册!";
                    lbResult.ForeColor = Color.Red;
                    this.btnConfirm.Enabled = true;
                }
                else
                {
                    if (SoftwareRegistryManager.Instance.IsSoftwareRegistered())
                    {
                        this.btnConfirm.Enabled = false;
                        this.lbResult.Text = "您已注册！";
                        this.lbResult.ForeColor = Color.Green;
                        this.txtCode.Text = SoftwareRegistryManager.Instance.RegistryCode;
                        this.txtCode.Enabled = false;
                        this.btnConfirm.Enabled = false;
                    }
                    else if (SoftwareRegistryManager.Instance.IsOutOfDate())
                    {
                        lbResult.Text = "您的有效期已过，请联系管理员重新注册!";
                        lbResult.ForeColor = Color.Red;
                        this.txtCode.Enabled = true;
                        this.btnConfirm.Enabled = true;
                    }
                    else
                    {
                        int remainDay = SoftwareRegistryManager.Instance.GetRemainDays();
                        lbResult.Text = string.Format("您已注册，有效期{0}天", remainDay);
                        lbResult.ForeColor = Color.Red;
                        this.txtCode.Text = SoftwareRegistryManager.Instance.RegistryCode;
                        this.txtCode.Enabled = true;
                        this.btnConfirm.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtCode.Text))
                {
                    MessageBox.Show("请输入注册码！");
                    return;
                }
                if (SoftwareRegistryManager.Instance.isRegisterCodeLegal(this.txtCode.Text))
                {
                    //将注册码写入注册文件
                    SoftwareRegistryManager.Instance.SetRegistryCode(this.txtCode.Text);
                    int days = SoftwareRegistryManager.Instance.GetRemainDays();
                    if (days == -1)
                    {
                        this.btnConfirm.Enabled = false;
                        this.lbResult.Text = "您已成功注册！";
                        this.lbResult.ForeColor = Color.Green;
                    }
                    else
                    {
                        this.btnConfirm.Enabled = true;
                        this.lbResult.Text = string.Format("您已注册，有效期{0}天！", days);
                        this.lbResult.ForeColor = Color.Red;
                    }

                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FrmRegister_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.isClosed = true;
        }
    }
}
